﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess;
using DataAccess.Exceptions;
using Model;

namespace Business.WEB
{
    public class LoginManager
    {
        public User Login(string username, string password)
        {
            try
            {
                if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
                {
                    throw new ValidationException("Los campos no pueden estar vacios");
                }
                User user = UserDataAccess.UserExists(username, password);
                if (user == null)
                {
                  throw new BusinessException("Usuario y/o Contraseña Incorrectos.");
                }
                else return user;   
            }
            catch (Exception)
            {
                throw new BusinessException("Usuario y/o Contraseña Incorrectos.");
            }
        }
        public User UserType(User user)
        {
            user = UserDataAccess.GetUserByUsername(user.Username);
            return user;
        }
        public User LoginWEB(string username, string password)
        {
            try
            {
                if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
                {
                    throw new ValidationException("Los campos no pueden estar vacios");
                }
                User user = UserDataAccess.UserExists(username, password);
                if (user == null || user.Tipo == "Administrator")
                {
                    throw new BusinessException("Usuario y/o Contraseña Incorrectos.");
                }
                
                else return user;
            }
            catch (Exception)
            {
                throw new BusinessException("Usuario y/o Contraseña Incorrectos.");
            }
        }
    }
}
